package org.homly.admin.action.product;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.homly.sql.SQLTool;

public class GetProductListAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String typeId = request.getParameter("typeId");
		String brandName = request.getParameter("brandName");
		String sql = " select * from product ";
		String where = " where product_status=1 ";
		String orderBy = " order by config_id asc,product_sort_no desc ";
		if(typeId != null && typeId.matches("\\d+")) {
			if(Integer.parseInt(typeId) > -999) {
				where += " and config_id=" + typeId;			
			}
		}
		if(brandName != null && !brandName.equals("")) {
			where += " and product_brand_name='" + brandName + "' ";
		}
		sql += where + orderBy;
		List<HashMap<String, String>> list = SQLTool.query(sql);
		JSONArray jsonArray = JSONArray.fromObject(list);
		response.getWriter().print(jsonArray);
		return null;
	}
}
